package com.mustafaiev.tair.autobase.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.mustafaiev.tair.autobase.db.DBConnector;
import com.mustafaiev.tair.autobase.web.command.AutobaseServletCommand;
import com.mustafaiev.tair.autobase.web.command.DriverListCommand;
import com.mustafaiev.tair.autobase.web.command.LoginCommandImpl;
import com.mustafaiev.tair.autobase.web.command.LogoutCommandImpl;
import com.mustafaiev.tair.autobase.web.command.OrderCommandImpl;
import com.mustafaiev.tair.autobase.web.command.OrderEditCommandImpl;
import com.mustafaiev.tair.autobase.web.command.OrderListCommandImpl;
import com.mustafaiev.tair.autobase.web.command.RegisterCommandImpl;

public class AutobaseServlet extends javax.servlet.http.HttpServlet {

    private static final Logger LOG = Logger.getLogger(AutobaseServlet.class.getName());

    /**
     * Map container for Command objects
     */
    private final Map<String, AutobaseServletCommand> commands = new HashMap<String, AutobaseServletCommand>();

    @Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);
        final Properties properties = new Properties();

        commands.put("editOrder", new OrderEditCommandImpl());

        commands.put("/admin/drivers.do", new DriverListCommand());
        commands.put("/admin.do", new LoginCommandImpl());
        commands.put("/admin/orders.do", new OrderListCommandImpl());
        commands.put("/admin/logout.do", new LogoutCommandImpl());

        commands.put("/login.do", new LoginCommandImpl());
        commands.put("/register.do", new RegisterCommandImpl());
        commands.put("/createOrder.do", new OrderCommandImpl());
        
        try {
            DBConnector.getInstance().start();
            properties.load(AutobaseServlet.class
                    .getResourceAsStream("/configurations/log4j.properties"));

        } catch (SQLException e) {
            LOG.error(e);
        } catch (IOException e) {
            LOG.error("Log4j properties file not found: " + e);
        }
        PropertyConfigurator.configure(properties);
    }

    @Override
    protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
            throws ServletException, IOException {

        final String action = req.getParameter("action");
        final String uri = req.getRequestURI();
        final AutobaseServletCommand servletCommand = commands.get(action == null ? uri : action);
        if (servletCommand == null) {
            getServletContext().getRequestDispatcher("/404.html").forward(req, resp);
            
        } else {
            servletCommand.processPost(req, resp);
        }
    }

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
            throws ServletException, IOException {
        
        Locale newLocale = new Locale("ru-RU");
        final String action = req.getParameter("action");
        final String uri = req.getRequestURI();
        final AutobaseServletCommand servletCommand = commands.get(action == null ? uri : action);

        if (servletCommand == null) {
            getServletContext().getRequestDispatcher("/404.html").forward(req, resp);
        } else {
            servletCommand.processGet(req, resp);
        }

    }

    @Override
    public void destroy() {
        super.destroy();
        DBConnector.getInstance().stop();
    }
}